package com.beverage.lemonade;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.actionbarsherlock.app.SherlockActivity;
import com.beverage.lemonade.databse.DataConnector;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class PeriodListActivity extends SherlockActivity {
	private DataConnector connector;
	List<Map<String, Object>> InfoList = new ArrayList<Map<String, Object>>();
	private ListView periodList;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTheme(R.style.Theme_Styled);

		setContentView(R.layout.activity_period_list);
		connector = new DataConnector(this);
		periodList = (ListView) findViewById(R.id.period_list);
		periodList.setAdapter(new CustomAdapter(this));

	}

	class ViewHolder {
		TextView time;
		TextView date;
		TextView span;
	}

	class CustomAdapter extends BaseAdapter {
		private LayoutInflater layoutInflater_;
		List<Map<String, Object>> objects = new ArrayList<Map<String, Object>>();

		public CustomAdapter(Context context) {
			super();
			layoutInflater_ = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			this.objects = connector.getPeriodList();

		}

		public View getView(int position, View convertView, ViewGroup parent) {

			Map<String, Object> map_view = getItem(position);
			ViewHolder holder;

			if (convertView == null) {
				convertView = layoutInflater_.inflate(
						R.layout.period_list_container, null);
				holder = new ViewHolder();
				holder.date = (TextView) convertView.findViewById(R.id.date);
				holder.time = (TextView) convertView.findViewById(R.id.time);
				holder.span = (TextView) convertView.findViewById(R.id.span);
				holder.span.setText(String.valueOf(map_view.get("Span")));
				holder.time.setText(String.valueOf(map_view.get("Time")));
				holder.date.setText(String.valueOf(map_view.get("Date")));
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			return convertView;
		}

		public int getCount() {
			// TODO Auto-generated method stub
			return objects.size();
		}

		public Map<String, Object> getItem(int position) {
			// TODO Auto-generated method stub
			return objects.get(position);
		}

		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

	}
}
